package cn.tedu.submarine;
//超类
import java.awt.*;
import java.util.Random;
import javax.swing.ImageIcon;

/**海洋对象*/
public abstract class SeaObject {
    public static final int LIVE = 0;
    public static final int DEAD = 1;
    private int state = LIVE;
    private int width;
    private int height;
    private int x;
    private int y;
    private int speed;
    /**MineSubmarine   ObserveSubmarine    TorpedoSubmarine*/
    public SeaObject(int width,int height){
        this.width = width;
        this.height = height;
        x = -width;
        Random rand = new Random();//随机数对象
        y = rand.nextInt(World.HEIGHT-height-150+1)+150;
        speed = rand.nextInt(3)+1;
    }
    /**Mine   Bomb Battleship*/
    public SeaObject(int width,int height, int x,int y,int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }
    /** 海洋对象移动*/
   public abstract void move();
   /**判断对象的状态*/
   public boolean isLive(){
       return  state == LIVE;   //表示为活着的，返回true
   }
   public boolean isDead(){
       return  state == DEAD;
   }

   /**获取对象图片*/
   public abstract ImageIcon getImage();

   /** 画对象*/
   public void paintImage(Graphics g){
       if(this.isLive()){
           this.getImage().paintIcon(null,g,this.x,this.y);
       }
   }
    /** 检测潜艇越界*/
    public boolean isOutOfBounds(){
        return this.x>=World.WIDTH;
    }
    /** 检测碰撞  */
    public boolean isHit(SeaObject other){//this:潜艇   other：炸弹
        int x1 = this.x- other.width;//x1:潜艇的x-炸弹的宽
        int x2 = this.x+this.width;//x2:潜艇的x+炸弹的宽
        int y1 = this.y- other.height;//y1:潜艇的y-炸弹的高
        int y2 = this.y+other.height;//y2:潜艇的y+炸弹的高
        int x = other.x;
        int y = other.y;
        return x>=x1 && x<=x2 && y>=y1 && y<=y2;//x在x1与x2之间，并且，y再y1与y2之间极为撞上
    }

    /** 海洋对象死 */
    public void goDead(){
        state = DEAD;//将状态改为DEAD
    }


    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }
}
